package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;
import core.model.Categoria;
import core.model.Empresa;
import core.model.Producto;
import core.model.Subcategoria;


/**
 * @author marioromero
 * Ventana administrador que gestiona los productos
 *
 */
public class panelAdministradorGestionProductos extends javax.swing.JPanel
		implements ActionListener, ItemListener {

	/**
	 * variables estaticas
	 */
	private static final long serialVersionUID = 8886964067394189532L;
	private static final int OK_CANCEL_OPTION = 0;
	private static final int PLAIN_MESSAGE = 0;

	
	/** Constructor del panel  */

	public panelAdministradorGestionProductos() {
		initComponents();
	}

	private void initComponents() {

		panelCentro2 = new javax.swing.JPanel();
        cbCategoria = new javax.swing.JComboBox();
        tfCantidad = new javax.swing.JTextField();
        eNombreProducto = new javax.swing.JLabel();
        eCodigoProducto = new javax.swing.JLabel();
        bCancelar = new javax.swing.JButton();
        eSubcategoriaProducto1 = new javax.swing.JLabel();
        bAgregar = new javax.swing.JButton();
        tfNombre = new javax.swing.JTextField();
        eCategoriaProducto = new javax.swing.JLabel();
        eUnidad = new javax.swing.JLabel();
        eCantidadProducto = new javax.swing.JLabel();
        cbSubcategoria = new javax.swing.JComboBox();
        eSubcategoriaProducto = new javax.swing.JLabel();
        tfCodigoBarras = new javax.swing.JTextField();
        tfUnidad = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tadescripcion = new javax.swing.JTextArea();
        eSuper = new javax.swing.JLabel();
        ePrecio = new javax.swing.JLabel();
        cbSuper = new javax.swing.JComboBox();
        tfPrecio = new javax.swing.JTextField();
        botonSalir = new javax.swing.JButton();
        bTitulo = new javax.swing.JButton();
        rbSugerencias = new javax.swing.JRadioButton();
        rbGestion = new javax.swing.JRadioButton();

		setBackground(new java.awt.Color(255, 255, 255));

		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.setBackground(new java.awt.Color(255, 255, 255));
		botonSalir.addActionListener(this);

		panelCentro2.setBackground(new java.awt.Color(255, 255, 255));

		

		eCategoriaProducto.setBackground(new java.awt.Color(255, 255, 255));
		eCategoriaProducto.setFont(new java.awt.Font("Lucida Handwriting", 1,
				13)); // NOI18N
		eCategoriaProducto.setForeground(new java.awt.Color(102, 102, 102));
		eCategoriaProducto.setText("CATEGORIA");

		eSubcategoriaProducto.setBackground(new java.awt.Color(255, 255, 255));
		eSubcategoriaProducto.setFont(new java.awt.Font("Lucida Handwriting",
				1, 13)); // NOI18N
		eSubcategoriaProducto.setForeground(new java.awt.Color(102, 102, 102));
		eSubcategoriaProducto.setText("SUBCATEGORIA");

		eCantidadProducto.setBackground(new java.awt.Color(255, 255, 255));
		eCantidadProducto
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eCantidadProducto.setForeground(new java.awt.Color(102, 102, 102));
		eCantidadProducto.setText("CANTIDAD");

		eSubcategoriaProducto1.setBackground(new java.awt.Color(255, 255, 255));
		eSubcategoriaProducto1.setFont(new java.awt.Font("Lucida Handwriting",
				1, 13)); // NOI18N
		eSubcategoriaProducto1.setForeground(new java.awt.Color(102, 102, 102));
		eSubcategoriaProducto1.setText("DESCRIPCION");

		eNombreProducto.setBackground(new java.awt.Color(255, 255, 255));
		eNombreProducto.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eNombreProducto.setForeground(new java.awt.Color(102, 102, 102));
		eNombreProducto.setText("NOMBRE");

		eCodigoProducto.setBackground(new java.awt.Color(255, 255, 255));
		eCodigoProducto.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eCodigoProducto.setForeground(new java.awt.Color(102, 102, 102));
		eCodigoProducto.setText("CODIGO DE BARRAS");

		eUnidad.setBackground(new java.awt.Color(255, 255, 255));
		eUnidad.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eUnidad.setForeground(new java.awt.Color(102, 102, 102));
		eUnidad.setText("UNIDAD");	

		tadescripcion.setColumns(20);
		tadescripcion.setRows(5);
		tadescripcion.setFont(new java.awt.Font("Calibri", 0, 13));
		tadescripcion.setEditable(true);
		jScrollPane1.setViewportView(tadescripcion);
		// RELLENAR COMBOBOX
				List<String> listaNombresCategorias = new ArrayList<String>();
				List<Categoria> categorias = (List<Categoria>) obtenerCategorias();

				Iterator<Categoria> iterator = categorias.iterator();
				while (iterator.hasNext()) {
					Categoria categoria = iterator.next();
					listaNombresCategorias.add(String.valueOf(categoria.getNombre()));
				}
				String[] categoriasArrayString = new String[listaNombresCategorias
						.size()];
				listaNombresCategorias.toArray(categoriasArrayString);

				cbCategoria.setModel(new javax.swing.DefaultComboBoxModel<String>(
						categoriasArrayString));
				// FIN
		// FIN
		cbCategoria.addItemListener(this);
		

		

		bAgregar.setBackground(new java.awt.Color(255, 255, 255));
		bAgregar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bAgregar.setForeground(new java.awt.Color(243, 15, 120));
		bAgregar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonAgregar.png")); // NOI18N
		bAgregar.setBorder(null);
		bAgregar.addActionListener(this);
		
		bCancelar.setBackground(new java.awt.Color(255, 255, 255));
		bCancelar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bCancelar.setForeground(new java.awt.Color(243, 15, 120));
		bCancelar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonCancelar.png")); // NOI18N
		bCancelar.setBorder(null);
		bCancelar.addActionListener(this);
		
		eSuper.setBackground(new java.awt.Color(255, 255, 255));
        eSuper.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        eSuper.setForeground(new java.awt.Color(102, 102, 102));
        eSuper.setText("EMPRESA");

        ePrecio.setBackground(new java.awt.Color(255, 255, 255));
        ePrecio.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        ePrecio.setForeground(new java.awt.Color(102, 102, 102));
        ePrecio.setText("PRECIO");
        ePrecio.setToolTipText("");

        VentanaManager ventanaManager= VentanaFactory.getInstance();
        List<Empresa> listaNombresEmpresas =ventanaManager.getEmpresas();
		String[] empresasArrayString = new String[listaNombresEmpresas.size()];
		for(int i=0;i<listaNombresEmpresas.size();i++){
			
			empresasArrayString[i]=listaNombresEmpresas.get(i).getNombre().toString();
		}

		cbSuper.setModel(new javax.swing.DefaultComboBoxModel<String>(
				empresasArrayString));
		// FIN

        tfPrecio.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        tfPrecio.setToolTipText("");

		

        org.jdesktop.layout.GroupLayout panelCentro2Layout = new org.jdesktop.layout.GroupLayout(panelCentro2);
        panelCentro2.setLayout(panelCentro2Layout);
        panelCentro2Layout.setHorizontalGroup(
            panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentro2Layout.createSequentialGroup()
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelCentro2Layout.createSequentialGroup()
                        .add(93, 93, 93)
                        .add(eNombreProducto))
                    .add(panelCentro2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(eCantidadProducto)
                            .add(eCodigoProducto)
                            .add(eCategoriaProducto)
                            .add(eSubcategoriaProducto)
                            .add(eSuper)
                            .add(ePrecio)
                            .add(eSubcategoriaProducto1))))
                .add(29, 29, 29)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(tfCodigoBarras, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tfNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 369, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(panelCentro2Layout.createSequentialGroup()
                        .add(tfCantidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(eUnidad)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(tfUnidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(cbCategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 250, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cbSubcategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 250, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cbSuper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 250, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tfPrecio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(panelCentro2Layout.createSequentialGroup()
                        .add(17, 17, 17)
                        .add(bAgregar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(50, 50, 50)
                        .add(bCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelCentro2Layout.setVerticalGroup(
            panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentro2Layout.createSequentialGroup()
                .add(25, 25, 25)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eNombreProducto)
                    .add(tfNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tfCodigoBarras, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(eCodigoProducto))
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eCantidadProducto)
                    .add(tfCantidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(eUnidad)
                    .add(tfUnidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eCategoriaProducto)
                    .add(cbCategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(23, 23, 23)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eSubcategoriaProducto)
                    .add(cbSubcategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eSuper)
                    .add(cbSuper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(ePrecio)
                    .add(tfPrecio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(eSubcategoriaProducto1))
                .add(32, 32, 32)
                .add(panelCentro2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bAgregar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(48, 48, 48))
        );

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png"));
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));
		bTitulo.addActionListener(this);

		rbSugerencias.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		rbSugerencias.setForeground(new java.awt.Color(243, 15, 120));
		rbSugerencias.setText("SUGERENCIAS");
		rbSugerencias.addItemListener(this);
		rbSugerencias.setBackground(new java.awt.Color(255, 255, 255));

		rbGestion.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		rbGestion.setForeground(new java.awt.Color(243, 15, 120));
		rbGestion.setText("GESTION");
		rbGestion.setSelected(true);
		rbGestion.setEnabled(false);
		rbGestion.setBackground(new java.awt.Color(255, 255, 255));

	
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(rbSugerencias)
                    .add(rbGestion))
                .add(18, 18, 18)
                .add(panelCentro2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(50, 50, 50))
            .add(layout.createSequentialGroup()
                .addContainerGap(129, Short.MAX_VALUE)
                .add(bTitulo)
                .add(87, 87, 87)
                .add(botonSalir))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(botonSalir)
                        .add(62, 62, 62))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(bTitulo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(panelCentro2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(rbSugerencias)
                        .add(18, 18, 18)
                        .add(rbGestion)
                        .add(0, 0, Short.MAX_VALUE))))
        );
	}
	
	
	private Subcategoria obtenerSubcategoria (String nombre_subcategoria) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		Subcategoria subcategoria = ventanaManager.getSubcategoriaByName(nombre_subcategoria);
		return subcategoria;
	}

	private List<Categoria> obtenerCategorias() {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Categoria> listaCategorias = ventanaManager.getAllCategorias();
		return listaCategorias;
	}

	private List<Subcategoria> obtenerSubcategorias(int cod_categoria) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Subcategoria> subcategorias = ventanaManager
				.getSubcategoriaByCodeCategoria(cod_categoria);
		return subcategorias;
	}

	 private javax.swing.JButton bAgregar;
	    private javax.swing.JButton bCancelar;
	    private javax.swing.JButton bTitulo;
	    private javax.swing.JButton botonSalir;
	    private javax.swing.JComboBox cbCategoria;
	    private javax.swing.JComboBox cbSubcategoria;
	    private javax.swing.JComboBox cbSuper;
	    private javax.swing.JLabel eCantidadProducto;
	    private javax.swing.JLabel eCategoriaProducto;
	    private javax.swing.JLabel eCodigoProducto;
	    private javax.swing.JLabel eNombreProducto;
	    private javax.swing.JLabel ePrecio;
	    private javax.swing.JLabel eSubcategoriaProducto;
	    private javax.swing.JLabel eSubcategoriaProducto1;
	    private javax.swing.JLabel eSuper;
	    private javax.swing.JLabel eUnidad;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JPanel panelCentro2;
	    private javax.swing.JRadioButton rbGestion;
	    private javax.swing.JRadioButton rbSugerencias;
	    private javax.swing.JTextArea tadescripcion;
	    private javax.swing.JTextField tfCantidad;
	    private javax.swing.JTextField tfCodigoBarras;
	    private javax.swing.JTextField tfNombre;
	    private javax.swing.JTextField tfPrecio;
	    private javax.swing.JTextField tfUnidad;
	    private JFrame actual;
	    private JFrame nuevo;
	    private JPanel estePanel = this;

	    
	    

	/* (non-Javadoc)
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == rbSugerencias) {

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorSugerencias());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}
		if (e.getSource() == cbCategoria) {

			/*if (cbCategoria.getSelectedIndex() != 0) {

				bBuscarSubcategoria.setEnabled(true);
			}
			if (cbCategoria.getSelectedIndex() == 0) {
				bBuscarSubcategoria.setEnabled(false);
			}
*/
			cbSubcategoria.addItemListener(this);
			cbSubcategoria.setEnabled(true);

			String nombreCategoria = (String) cbCategoria.getSelectedItem();
			System.out.println(nombreCategoria);
			VentanaManager ventanaManager = VentanaFactory.getInstance();
			Categoria categoria = ventanaManager
					.getCategoriaByName(nombreCategoria);

			// RELLENAR COMBOBOX SUBCATEGORIA
			List<String> listaNombresSubcategorias = new ArrayList<String>();
			List<Subcategoria> subcategorias = obtenerSubcategorias(categoria
					.getCodigo_categoria());
			
			Iterator<Subcategoria> iterator = subcategorias.iterator();
			while (iterator.hasNext()) {
				Subcategoria subcategoria = iterator.next();
				listaNombresSubcategorias.add(String.valueOf(subcategoria
						.getNombre()));
			}
			String[] SubcategoriasArrayString = new String[listaNombresSubcategorias
					.size()];
			listaNombresSubcategorias.toArray(SubcategoriasArrayString);

			cbSubcategoria.setModel(new javax.swing.DefaultComboBoxModel(
					SubcategoriasArrayString));
			// FIN
		}
	

	}

	

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bAgregar) {
			
			if((tfNombre.getText().length()==0)||(tfCodigoBarras.getText().length()==0)||(tfCantidad.getText().length()==0)
					||(tfUnidad.getText().length()==0)||(cbSubcategoria.getSelectedIndex()<0)||(tfPrecio.getText().length()==0)
					||(tadescripcion.getText().length()==0)){
				Icon icono = new ImageIcon("src/GUI/resources/error.gif");	
				
				JOptionPane.showOptionDialog(null, "Es necesario rellenar todos los campos",
				"Agregar Producto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
						new Object[] { "ACEPTAR" }, null);
			}else{
				
				VentanaManager ventanaManager=VentanaFactory.getInstance();
				Subcategoria subcategoria=ventanaManager.getSubcategoriaByCode(cbSubcategoria.getSelectedIndex());
				Producto producto=new Producto(tfCodigoBarras.getText(), tfNombre.getText(), tadescripcion.getText(),Double.parseDouble(tfCantidad.getText()), tfUnidad.getText(),subcategoria.getCodigo_subcategoria());
				ventanaManager.insertProducto(producto);
				ventanaManager.insertProductoSupermercado(cbSuper.getSelectedIndex()+1, tfCodigoBarras.getText(),Float.parseFloat(tfPrecio.getText()));
				
				Icon icono = new ImageIcon("src/GUI/resources/aceptar.gif");	
				
				JOptionPane.showOptionDialog(null, "Producto insertado con exito",
				"Agregar Producto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
						new Object[] { "ACEPTAR" }, null);
				actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
				actual.getContentPane().remove(0);
				actual.getContentPane().add(
						new panelAdministradorGestionProductos());
				actual.setPreferredSize(new Dimension(680, 550));
				actual.pack();
				actual.repaint();
				UtilidadesGUI.CentrarPantalla(actual);
				
			}
			

		}
		
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(23));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bCancelar) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAdministradorPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

	}

}
